﻿using System;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using SilverlightViewport.Common.Interop;

namespace SilverlightViewport.Common
{
    public class WindowlessContainer : Control,
                                       IOleInPlaceSiteWindowless, 
                                       IOleClientSite, 
                                       IOleInPlaceFrame
    {
        protected const int S_OK = 0;
        protected const int E_FAIL = unchecked((int)0x80004005);
        protected const int E_NOINTERFACE = unchecked((int)0x80004002);
        protected const int E_NOTIMPL = unchecked((int)0x80004001);
        protected const int S_FALSE = 1;

        public WindowlessContainer()
        {
            CreateControl();
        }

        #region IOleClientSite Members

        public virtual int SaveObject()
        {
            return E_NOTIMPL;
        }

        public virtual int GetMoniker(uint dwAssign, uint dwWhichMoniker, out IMoniker ppmk)
        {
            ppmk = null;
            return E_NOTIMPL;
        }

        public virtual int GetContainer(out IOleContainer ppContainer)
        {
            ppContainer = null;
            return E_NOTIMPL;
        }

        public virtual int ShowObject()
        {
            return S_OK;
        }

        public virtual int OnShowWindow(int fShow)
        {
            return E_NOTIMPL;
        }

        public virtual int RequestNewObjectLayout()
        {
            return E_NOTIMPL;
        }

        #endregion

        #region IOleInPlaceSiteWindowless Members

        public virtual int GetWindow(out IntPtr phwnd)
        {
            phwnd = Handle;

            if (phwnd == IntPtr.Zero)
                return E_FAIL;
            else
                return S_OK;
        }

        public virtual int ContextSensitiveHelp(int fEnterMode)
        {
            return E_NOTIMPL;
        }

        public virtual int CanInPlaceActivate()
        {
            return S_OK;
        }

        public virtual int OnInPlaceActivate()
        {
            return S_OK;
        }

        public virtual int OnUIActivate()
        {
            return S_OK;
        }

        public virtual int GetWindowContext(out IOleInPlaceFrame ppFrame, out IOleInPlaceUIWindow ppDoc, RECT[] lprcPosRect, RECT[] lprcClipRect, OLEINPLACEFRAMEINFO[] lpFrameInfo)
        {
            ppDoc = null;
            ppFrame = null;
            return E_NOTIMPL;
        }

        public virtual int Scroll(SIZE scrollExtant)
        {
            return E_NOTIMPL;
        }

        public virtual int OnUIDeactivate(int fUndoable)
        {
            return S_OK;
        }

        public virtual int OnInPlaceDeactivate()
        {
            return S_OK;
        }

        public virtual int DiscardUndoState()
        {
            return E_NOTIMPL;
        }

        public virtual int DeactivateAndUndo()
        {
            return E_NOTIMPL;
        }

        public virtual int OnPosRectChange(RECT[] lprcPosRect)
        {
            return E_NOTIMPL;
        }

        public virtual int OnInPlaceActivateEx(out int pfNoRedraw, uint dwFlags)
        {
            pfNoRedraw = 0;
            return S_OK;
        }

        public virtual int OnInPlaceDeactivateEx(int fNoRedraw)
        {
            return E_NOTIMPL;
        }

        public virtual int RequestUIActivate()
        {
            return E_NOTIMPL;
        }

        public virtual int CanWindowlessActivate()
        {
            return S_OK;
        }

        public virtual int GetCapture()
        {
            return S_OK;
        }

        public virtual int SetCapture(int fCapture)
        {
            return S_OK;
        }

        public virtual int GetFocus()
        {
            return S_OK;
        }

        public virtual int SetFocus(int fFocus)
        {
            return S_OK;
        }

        public virtual int GetDC(RECT[] pRect, uint grfFlags, out IntPtr phDC)
        {
            phDC = IntPtr.Zero;
            return S_OK;
        }

        public virtual int ReleaseDC(IntPtr hDC)
        {
            return E_NOTIMPL;
        }

        public virtual int InvalidateRect(RECT[] pRect, int fErase)
        {
            return S_OK;
        }

        public virtual int InvalidateRgn(IntPtr hRGN, int fErase)
        {
            return S_OK;
        }

        public virtual int ScrollRect(int dx, int dy, RECT[] pRectScroll, RECT[] pRectClip)
        {
            return S_OK;
        }

        public virtual int AdjustRect(RECT[] prc)
        {
            return S_OK;
        }

        public virtual int OnDefWindowMessage(uint MSG, IntPtr wParam, IntPtr lParam, out int plResult)
        {
            plResult = 0;
            return S_OK;
        }

        #endregion

        #region IOleInPlaceFrame Members

        public virtual int GetBorder(RECT[] lprectBorder)
        {
            return E_NOTIMPL;
        }

        public virtual int RequestBorderSpace(RECT[] pborderwidths)
        {
            return E_NOTIMPL;
        }

        public virtual int SetBorderSpace(RECT[] pborderwidths)
        {
            return E_NOTIMPL;
        }

        public virtual int SetActiveObject(IOleInPlaceActiveObject pActiveObject, string pszObjName)
        {
            return S_OK;
        }

        public virtual int InsertMenus(IntPtr hmenuShared, OLEMENUGROUPWIDTHS[] lpMenuWidths)
        {
            return E_NOTIMPL;
        }

        public virtual int SetMenu(IntPtr hmenuShared, IntPtr HOLEMENU, IntPtr hwndActiveObject)
        {
            return S_OK;
        }

        public virtual int RemoveMenus(IntPtr hmenuShared)
        {
            return S_OK;
        }

        public virtual int SetStatusText(string pszStatusText)
        {
            return S_OK;
        }

        public virtual int EnableModeless(int fEnable)
        {
            return S_OK;
        }

        public virtual int TranslateAccelerator(MSG[] lpmsg, ushort wID)
        {
            return E_NOTIMPL;
        }

        #endregion
    }
}
